#include <stdio.h>
#include <memory.h>

static int access[26]; /* mask of access rights for each entity */

void assign_rights(int entities[], int num_entities, int rights, char mode)
{
    int i;
    for (i = 0; i < num_entities; i++)
    {
        int k = entities[i];
        if (mode == '=')
            access[k] = rights;
        else if (mode == '+')
            access[k] |= rights;
        else if (mode == '-')
            access[k] &= ~rights;
    }
}

void display_rights(int rights)
{
    int i;
    for (i = 0; i < 26; i++)
        if (rights & (1 << i))
            printf("%c", i + 'a');
}

void get_acl(const char *s)
{
    int entities[100], num_entities = 0;
    int rights = 0; /* bit-mask of access rights */
    char mode = 0;  /* '=', '+', '-' */
    char c;

    memset(access, 0, sizeof(access));
    while (c = *s++)
    {
        if (c == ',')
        {
            assign_rights(entities, num_entities, rights, mode);
            num_entities = 0;
            rights = 0;
        }
        else if (c == '=')
            mode = '=';
        else if (c == '+')
            mode = '+';
        else if (c == '-')
            mode = '-';
        else if (c >= 'A' && c <= 'Z')
            entities[num_entities++] = c - 'A';
        else if (c >= 'a' && c <= 'z')
            rights |= 1 << (c - 'a');
    }
    assign_rights(entities, num_entities, rights, mode);
}

int main()
{
    char line[100];
    int t;
    for (t = 1; fgets(line, sizeof(line), stdin); t++)
    {
        int k, last_rights = 0;

        if (line[0] == '#' && line[1] < 32)
            break;

        get_acl(line);

        printf("%d:", t);
        for (k = 0; k < 26; k++)
        {
            int rights = access[k];
            if (rights == 0)
                continue;
            if (rights != last_rights)
                display_rights(last_rights);
            printf("%c", 'A' + k);
            last_rights = rights;
        }
        display_rights(last_rights);
        printf("\n");
    }
    return 0;
}
